Collections

A collection may store a number of documents. A collection is analogous to a table of an RDBMS. A collection may store documents those who are not same in structure. This is possible because MongoDB is a Schema-free database. In a relational database like MySQL, a schema defines the organization / structure of data in a database. MongoDB does not require such a set of formula defining structure of data. So, it is quite possible to store documents of varying structures in a collection. Practically, you don't need to define a column and it's datatype unlike in RDBMS, while working with MongoDB.

Valid collection names
  • Collection names must begin with letters or an underscore.
  • A Collection name may contain numbers.
  • You can't use "$" character within the name of a collection. "$" is reserved.
  • A Collection name must not exceed 128 characters. It will be nice if you keep it within 80/90 characters.
  • Using a "." (dot) notation, collections can be organized in named groups. For example, tutorials.php and tutorials.javascript both belong to tutorials. This mechanism is called as collection namespace which is for user primarily. Databases don't have much to do with it.
How to create Collections

The createCollection() Method
MongoDB db.createCollection(name, options) is used to create collection.Basic 
syntax of createCollection() command is as 

db.createCollection(name, options)

use mydb
db.createCollection("myCollection")

In the command, name is name of collection to be created. Options is a document and is used to specify configuration of collection. You can check the created collection with the show collections command. 

show collections

https://www.linode.com/docs/databases/mongodb/install-mongodb-on-ubuntu-16-04/
https://university.mongodb.com/mercury/M001/2020_May_26/chapter/Chapter_1_Introduction/lesson/5950156a42874dda44aa1faa/lecture

No comments:

Post a Comment